package spring.cloud.tasks.api.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Setter
@Getter
public class TaskConfiguration {
    private String taskId;
    private String taskType;
    private String taskClass;
    private String description;
    //
    private Boolean enabled;
    //
    private String cronExpression;
    private String pauseDatePeriod;
    private String pauseTimePeriod;
    //
    private Integer loadLevel;
    //
    private Integer taskItemTotalCount;
    private String taskItemParameters;
    private String taskParameter;
    //
    private String preferList;
    private Boolean useDisPreferList;

    //
    private Integer timeout4AlarmSeconds;
    private Integer timeoutSeconds;
    private Integer processCountIntervalSeconds;
    private Boolean showNormalLog;
    //

    private Boolean failover;
    private Boolean rerun;

    private <T> T getDefaultIfNull(T val, T def) {
        return val == null ? def : val;
    }

    public void setDefaultValues() {
        taskClass = getDefaultIfNull(taskClass, "");
        taskItemTotalCount = getDefaultIfNull(taskItemTotalCount, 1);

        cronExpression = getDefaultIfNull(cronExpression, "");
        pauseDatePeriod = getDefaultIfNull(pauseDatePeriod, "");
        pauseTimePeriod = getDefaultIfNull(pauseTimePeriod, "");
        taskItemParameters = getDefaultIfNull(taskItemParameters, "");
        taskParameter = getDefaultIfNull(taskParameter, "");
        processCountIntervalSeconds = getDefaultIfNull(processCountIntervalSeconds, 300);
        description = getDefaultIfNull(description, "");
        timeout4AlarmSeconds = timeout4AlarmSeconds == null || timeout4AlarmSeconds < 0 ? 0 : timeout4AlarmSeconds;
        timeoutSeconds = timeoutSeconds == null || timeoutSeconds < 0 ? 0 : timeoutSeconds;

        showNormalLog = getDefaultIfNull(showNormalLog, Boolean.FALSE);

        loadLevel = getDefaultIfNull(loadLevel, 1);

        enabled = getDefaultIfNull(enabled, Boolean.FALSE);
        preferList = getDefaultIfNull(preferList, "");
        useDisPreferList = getDefaultIfNull(useDisPreferList, Boolean.TRUE);


        rerun = getDefaultIfNull(rerun, Boolean.FALSE);

    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        TaskConfiguration taskConfiguration = (TaskConfiguration) o;

        if (taskId != null ? !taskId.equals(taskConfiguration.taskId) : taskConfiguration.taskId != null)
            return false;
        if (taskClass != null ? !taskClass.equals(taskConfiguration.taskClass) : taskConfiguration.taskClass != null)
            return false;
        if (taskItemTotalCount != null ?
                !taskItemTotalCount.equals(taskConfiguration.taskItemTotalCount) :
                taskConfiguration.taskItemTotalCount != null)
            return false;

        if (cronExpression != null ? !cronExpression.equals(taskConfiguration.cronExpression) : taskConfiguration.cronExpression != null)
            return false;
        if (pauseDatePeriod != null ?
                !pauseDatePeriod.equals(taskConfiguration.pauseDatePeriod) :
                taskConfiguration.pauseDatePeriod != null)
            return false;
        if (pauseTimePeriod != null ?
                !pauseTimePeriod.equals(taskConfiguration.pauseTimePeriod) :
                taskConfiguration.pauseTimePeriod != null)
            return false;
        if (taskItemParameters != null ?
                !taskItemParameters.equals(taskConfiguration.taskItemParameters) :
                taskConfiguration.taskItemParameters != null)
            return false;
        if (taskParameter != null ? !taskParameter.equals(taskConfiguration.taskParameter) : taskConfiguration.taskParameter != null)
            return false;
        if (processCountIntervalSeconds != null ?
                !processCountIntervalSeconds.equals(taskConfiguration.processCountIntervalSeconds) :
                taskConfiguration.processCountIntervalSeconds != null)
            return false;
        if (failover != null ? !failover.equals(taskConfiguration.failover) : taskConfiguration.failover != null)
            return false;
        if (description != null ? !description.equals(taskConfiguration.description) : taskConfiguration.description != null)
            return false;
        if (timeout4AlarmSeconds != null ?
                !timeout4AlarmSeconds.equals(taskConfiguration.timeout4AlarmSeconds) :
                taskConfiguration.timeout4AlarmSeconds != null)
            return false;
        if (timeoutSeconds != null ?
                !timeoutSeconds.equals(taskConfiguration.timeoutSeconds) :
                taskConfiguration.timeoutSeconds != null)
            return false;
        if (showNormalLog != null ? !showNormalLog.equals(taskConfiguration.showNormalLog) : taskConfiguration.showNormalLog != null)
            return false;

        if (taskType != null ? !taskType.equals(taskConfiguration.taskType) : taskConfiguration.taskType != null)
            return false;

        if (loadLevel != null ? !loadLevel.equals(taskConfiguration.loadLevel) : taskConfiguration.loadLevel != null)
            return false;


        if (enabled != null ? !enabled.equals(taskConfiguration.enabled) : taskConfiguration.enabled != null)
            return false;
        if (preferList != null ? !preferList.equals(taskConfiguration.preferList) : taskConfiguration.preferList != null)
            return false;
        if (useDisPreferList != null ?
                !useDisPreferList.equals(taskConfiguration.useDisPreferList) :
                taskConfiguration.useDisPreferList != null)
            return false;


        return rerun != null ? rerun.equals(taskConfiguration.rerun) : taskConfiguration.rerun == null;
    }

    @Override
    public int hashCode() {
        int result = taskId != null ? taskId.hashCode() : 0;
        result = 31 * result + (taskClass != null ? taskClass.hashCode() : 0);
        result = 31 * result + (taskItemTotalCount != null ? taskItemTotalCount.hashCode() : 0);

        result = 31 * result + (cronExpression != null ? cronExpression.hashCode() : 0);
        result = 31 * result + (pauseDatePeriod != null ? pauseDatePeriod.hashCode() : 0);
        result = 31 * result + (pauseTimePeriod != null ? pauseTimePeriod.hashCode() : 0);
        result = 31 * result + (taskItemParameters != null ? taskItemParameters.hashCode() : 0);
        result = 31 * result + (taskParameter != null ? taskParameter.hashCode() : 0);
        result = 31 * result + (processCountIntervalSeconds != null ? processCountIntervalSeconds.hashCode() : 0);
        result = 31 * result + (failover != null ? failover.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (timeout4AlarmSeconds != null ? timeout4AlarmSeconds.hashCode() : 0);
        result = 31 * result + (timeoutSeconds != null ? timeoutSeconds.hashCode() : 0);
        result = 31 * result + (showNormalLog != null ? showNormalLog.hashCode() : 0);

        result = 31 * result + (taskType != null ? taskType.hashCode() : 0);

        result = 31 * result + (loadLevel != null ? loadLevel.hashCode() : 0);


        result = 31 * result + (enabled != null ? enabled.hashCode() : 0);
        result = 31 * result + (preferList != null ? preferList.hashCode() : 0);
        result = 31 * result + (useDisPreferList != null ? useDisPreferList.hashCode() : 0);


        result = 31 * result + (rerun != null ? rerun.hashCode() : 0);

        return result;
    }

    public static class TaskType {
        public static final String TASK_TYPE_ITEM_TASK = "ITEM_TASK";
        public static final String TASK_TYPE_MESSAGE_QUEUE_TASK = "MESSAGE_QUEUE_TASK";
        public static final String TASK_TYPE_BATCH_TASK = "BATCH_TASK";
    }
}
